/**
 * jims
 */
package com.jims.master.modules.reg.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jims.master.common.utils.ChineseCharToEnUtils;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.modules.doctor.dao.ClinicMasterDao;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.finance.dao.RegistCostDao;
import com.jims.master.modules.finance.entity.RegistCost;
import com.jims.master.modules.oper.dao.PriceListDao;
import com.jims.master.modules.oper.entity.PriceList;
import com.jims.master.modules.reg.dao.ClinicForRegistDao;
import com.jims.master.modules.doctor.dao.PatMasterIndexDao;
import com.jims.master.modules.reg.dao.ClinicIndexDao;
import com.jims.master.modules.reg.entity.ClinicForRegist;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.reg.entity.ClinicAppoints;
import com.jims.master.modules.reg.dao.ClinicAppointsDao;

/**
 * 预约挂号Service
 * @author 赵宁
 * @version 2017-03-02
 */
@Service
@Transactional(readOnly = true)
public class ClinicAppointsService extends CrudService<ClinicAppointsDao, ClinicAppoints> {
	@Autowired
	private ClinicAppointsDao clinicAppointsDao;
	@Autowired
	private PatMasterIndexDao patMasterIndexDao;
	@Autowired
    private ClinicForRegistDao clinicForRegistDao;
	@Autowired
	private ClinicMasterDao clinicMasterDao;
	@Autowired
	private ClinicIndexDao clinicIndexDao;
	@Autowired
	private PriceListDao priceListDao;
	@Autowired
	private RegistCostDao registCostDao;

	public ClinicAppoints get(String id) {
		return super.get(id);
	}

	/**
	 * 查询预约号表
	 * @param date
	 * @return
	 * @author 赵宁
	 */
	public List<ClinicForRegist> findListRegist(String date,String clinicName,String deptId) {
		return clinicForRegistDao.findRegistTomorrow(date,clinicName,deptId,"","", UserUtils.getOrgId());
	}
	/**
	 * 保存预约数据
	 * @param clinicAppoints
	 * @param clinicIds
	 * @return
	 */
	@Transactional(readOnly = false)
	public int save(ClinicAppoints clinicAppoints,String clinicIds) {
		int code = 0;
		if(clinicIds!=null){
			String [] clinicId = clinicIds.split(",");
			for(int i=0;i<clinicId.length;i++){
				clinicAppoints.setRegistId(clinicId[i]);
				clinicAppoints.preInsert();
				code= clinicAppointsDao.insert(clinicAppoints);
				//更新号表
				clinicForRegistDao.updateRegistByAppoints(clinicId[i]);

			}
		}
		return code;
	}

	/**
	 * 预约确认
	 * @param clinicAppoints
	 * @return
	 */
	@Transactional(readOnly = false)
    public int confirmAppoints(ClinicAppoints clinicAppoints,String clinicCharge,String payType){
		  int code=0;
		//保存病人基本信息
          String patientId=savePatMaster(clinicAppoints);
        //保存病人诊疗信息&& 插入挂号收费数据
         code= saveClinicMaster(clinicAppoints,patientId,clinicCharge,payType);
        //删除预约信息
		 delete(clinicAppoints);
		  return code;
	}
	/**
	 * 保存病人基本信息
	 * @param clinicAppoints
	 * @return
	 * @author 赵宁
	 */
	@Transactional(readOnly = false)
	public String  savePatMaster(ClinicAppoints clinicAppoints){
		PatMasterIndex patMasterIndex = new PatMasterIndex();
		patMasterIndex.setOrgId(clinicAppoints.getOrgId());
		patMasterIndex.setName(clinicAppoints.getName());
		patMasterIndex.setInputCode(ChineseCharToEnUtils.getAllFirstLetter(patMasterIndex.getName()));
		patMasterIndex.setSex(clinicAppoints.getSex());
		patMasterIndex.setAge(clinicAppoints.getAge());
		patMasterIndex.setNation(clinicAppoints.getNation());
		patMasterIndex.setNationality(clinicAppoints.getNationality());
		patMasterIndex.setBirthdayDate(clinicAppoints.getBirthdayDate());
		patMasterIndex.setNativePlace(clinicAppoints.getNativePlace());
		patMasterIndex.setZipCode(clinicAppoints.getZipCode());
		patMasterIndex.setHomeplace(clinicAppoints.getHomeplace());
		patMasterIndex.setAddress(clinicAppoints.getAddress());
		patMasterIndex.setIdNo(clinicAppoints.getIdNo());
		patMasterIndex.setPhone(clinicAppoints.getPhone());
		patMasterIndex.setContacts(clinicAppoints.getContacts());
		patMasterIndex.setContactsAddress(clinicAppoints.getContactsAddress());
		patMasterIndex.setContactsPhone(clinicAppoints.getContactsPhone());
		patMasterIndex.setUnitInContract(clinicAppoints.getUnitInContract());
		patMasterIndex.setChargeType(clinicAppoints.getChargeType());
		if (patMasterIndex.getIsNewRecord()){
			patMasterIndex.preInsert();
			patMasterIndexDao.insert(patMasterIndex);
		}else{
			patMasterIndex.preUpdate();
			patMasterIndexDao.update(patMasterIndex);
		}
		return patMasterIndex.getId();
	}

	/**
	 * 保存病人就诊信息
	 * @param clinicAppoints
	 * @author 赵宁
	 */
	@Transactional(readOnly = false)
	public int  saveClinicMaster(ClinicAppoints clinicAppoints,String patientId,String clinicCharge,String payType){
		int code=0;
		ClinicMaster clinicMaster = new ClinicMaster();
		clinicMaster.setOrgId(clinicAppoints.getOrgId());
		clinicMaster.setRegistType("1");//预约挂号
		clinicMaster.setPatientId(patientId);
		clinicMaster.setVisitDate(clinicAppoints.getClinicForRegist().getClinicDate());
		clinicMaster.setFirstVisitIndicator(clinicAppoints.getFirstVisitIndicator());
		String date =DateUtils.formatDate(clinicAppoints.getClinicForRegist().getClinicDate());
		Integer  no= clinicMasterDao.getMaxNo(date,clinicAppoints.getOrgId());
		if(no!=null &&!no.equals("")){
			no=no+1;
		}else{
			no=1;
		}
		String visitDate = DateUtils.formatDate(clinicAppoints.getClinicForRegist().getClinicDate(), "yyyyMMdd");
		clinicMaster.setVisitNo(no+"");
		clinicMaster.setClinicNo(visitDate+clinicMaster.getVisitNo());
		clinicMaster.setRegistId(clinicAppoints.getRegistId());
		clinicMaster.setName(clinicAppoints.getName());
		clinicMaster.setSex(clinicAppoints.getSex());
		clinicMaster.setAge(clinicAppoints.getAge());
		clinicMaster.setIdentity(clinicAppoints.getIdentity());
		clinicMaster.setChargeType(clinicAppoints.getChargeType());
		clinicMaster.setUnitInContract(clinicAppoints.getUnitInContract());
		clinicMaster.setDeptId(clinicAppoints.getClinicIndex().getDeptId());
		clinicMaster.setDoctorId(clinicAppoints.getClinicIndex().getDoctorId());
		clinicMaster.setClinicType("0");
		clinicMaster.setClinicChage(Double.valueOf(clinicCharge));
		if (clinicMaster.getIsNewRecord()){
			clinicMaster.preInsert();
			//entity.updateOrgId();
			code=clinicMasterDao.insert(clinicMaster);
		}else{
			clinicMaster.preUpdate();
			code=clinicMasterDao.update(clinicMaster);
		}
		//插入挂号费用记录
		String clinicItemId = clinicAppoints.getClinicIndex().getClinicItemId();
		List<PriceList> priceLists=  priceListDao.findListPrice(clinicItemId);
		if(priceLists.size()>0){
			for(int j=0;j<priceLists.size();j++){
				PriceList  priceList=priceLists.get(j);
				RegistCost registCost = new RegistCost();
				registCost.setClinicId(clinicMaster.getId());
				registCost.setType(payType);
				registCost.setCost(priceList.getPrice()*priceList.getAmount());
				registCost.setSubjCode(priceList.getSubjCode());
				if (registCost.getIsNewRecord()){
					registCost.preInsert();
					registCostDao.insert(registCost);
				}else{
					registCost.preUpdate();
					registCostDao.update(registCost);
				}
			}
		}
		return code;
	}

	/**
	 * 预约修改保存数据
	 * @param clinicAppoints
	 * @return
	 * @author 赵宁
	 */
	@Transactional(readOnly = false)
    public int appointSave(ClinicAppoints clinicAppoints){
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		  int code=0;
		  String clinicIndexId=clinicAppoints.getClinicIndex().getId();
		  String timeDesc=clinicAppoints.getClinicForRegist().getTimeDesc();
		  Date date=clinicAppoints.getClinicForRegist().getClinicDate();
		  String clinicDate=format.format(date);
          List<ClinicForRegist> clinicForRegists = clinicForRegistDao.findRegistTomorrow(clinicDate,"","",clinicIndexId,timeDesc,UserUtils.getOrgId());
		  if(clinicForRegists==null || clinicForRegists.size()<=0){
			   return code;
		  } else{
			   ClinicForRegist clinicForRegist=clinicForRegists.get(0);
			   clinicAppoints.setRegistId(clinicForRegist.getId());
			   code=super.save(clinicAppoints);
			   return code;
		  }
	}
	public Page<ClinicAppoints> findPage(Page<ClinicAppoints> page, ClinicAppoints clinicAppoints) {
		return super.findPage(page, clinicAppoints);
	}

	@Transactional(readOnly = false)
	public int delete(ClinicAppoints clinicAppoints) {
		return super.delete(clinicAppoints);
	}
	
}